package cn.vastbase.jdbc.base;

import org.junit.Test;

import java.sql.*;

/**
 * <p>结果集API操作示例 </p>
 * <p>适用于Vastbase G100 2.2 Build 15.4 </p>
 */
public class ResultTestCase {

    /* 定义连接对象 */
    static Connection conn = null;
    static String cname = "org.postgresql.Driver";
    static String url = "jdbc:postgresql://172.16.55.175:6666/zxsdb";
    static String username = "zxs";
    static String passwd = "Vastdata#0730";
    /* 定义 SQL 语句执行对象 */
    static Statement stmt = null;
    static PreparedStatement pstmt = null;
    /* 定义结果集对象 */
    static ResultSet rs = null;

    public Connection conn() {
        try {
            Class.forName(cname);
            return conn = DriverManager.getConnection(url, username, passwd);
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("[FAIL] conn database fail." + e.getMessage());
        }
        return null;

    }

    public void close() {
        try {
            /* 关闭结果集对象 */
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close resultSet fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭连接 */
            conn.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close connection fail." + e.getMessage());
        }
    }

    /*
     CREATE TABLE staff(
      ID INT PRIMARY KEY     NOT NULL,
      NAME           TEXT    NOT NULL,
      AGE            INT     NOT NULL,
      ADDRESS        CHAR(50),
      SALARY         REAL);

     INSERT INTO staff(ID,NAME,AGE,ADDRESS,SALARY) VALUES
       (1,'Paul',32,'California',20000.00),
       (2,'Allen',25,'Texas',15000.00),
       (3,'Teddy',23,'Norway',20000.00),
       (4,'Mark',35,'Rich-Mond',65000.00);
     */

    /**
     * 可滚动结果集
     */
    @Test
    public void scrollResult() {
        try {
            if (conn() == null)
                return;

            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery("SELECT ID,NAME,AGE,ADDRESS,SALARY FROM STAFF");
            System.out.println("当前位置是否是第一行之前 ：" + rs.isBeforeFirst()); // true

            System.out.println("下一行");
            rs.next();// 下一行
            System.out.println("当前位置是否是第一行 ：" + rs.isFirst()); // true
            System.out.println("ID：" + rs.getString(1) + "，NAME：" + rs.getString(2)); // Paul

            System.out.println("移动到后一行");
            rs.last();
            System.out.println("ID：" + rs.getString(1) + "，NAME：" + rs.getString(2));
            System.out.println("当前位置是否是最后一行 ：" + rs.isLast()); // true

            System.out.println("下一行");
            rs.next();// 下一行
            System.out.println("当前位置是否是在最后一行之后 ：" + rs.isAfterLast()); // true


            System.out.println("移动到第3行");
            rs.absolute(3);
            System.out.println("ID：" + rs.getString(1) + "，NAME：" + rs.getString(2));

            System.out.println("上一行");
            rs.previous();
            System.out.println("ID：" + rs.getString(1) + "，NAME：" + rs.getString(2));
        } catch (SQLException e) {
            System.out.println("[FAIL] select fail." + e.getMessage());
        } finally {
            close();
        }


    }


    /**
     * 更新结果集
     */
    @Test
    public void updateResult() {
        try {
            if (conn() == null)
                return;
            boolean support = conn.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            System.out.println("数据库是否支持结果集参数设置：" + support);
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery("select id,name,age,address,salary from staff");

            // 插入行
            try {
                rs.moveToInsertRow();
                rs.updateInt(1, 5);
                rs.updateString(2, "李雷");
                rs.updateInt(3, 28);
                rs.updateString(4, "北京");
                rs.updateFloat(5, 30000.00f);
                rs.insertRow();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(rs.getString(2));
            // 移动到最后一行
            rs.last();
            // 更新最后一行
            try {
                rs.updateFloat(5, 40000.00f);
                rs.updateRow();
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 删除最后一行
            rs.deleteRow();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }


    /**
     * 敏感结果集
     */
    @Test
    public void sensitiveResult() {
        try {
            if (conn() == null)
                return;
            boolean support = conn.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            System.out.println("数据库是否支持结果集参数设置：" + support); // false
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }


}
